package com.aqie.common.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @Function:
 * @Author: aqie
 * @Date: 2019/4/5 11:44
 */
@Data
public class R<M> implements Serializable {
    private int code;
    private boolean success;
    private String message;

    private M data;

    public R() {
        code = 1;
        message = "ok";
        success = true;
    }

    public R(String msg){
        code = 0;
        message = msg;
        success = false;
    }

    public R(M result) {
        this();
        this.data = result;
    }

    public static <M> R<M> success() {
        return new R<M>();
    }


    // 成功时调用
    public static <M> R<M> success(M data) {
        return new R<M>(data);
    }

    public static <M> R<M> error(String msg){
        return new R<>(msg);
    }
    public static <M> R<M> error(CodeMsg codeMsg){
        return new R<M>(codeMsg);
    }

    private R(CodeMsg codeMsg){
        if (codeMsg == null) {
            return;
        }
        this.code = codeMsg.getCode();
        this.message = codeMsg.getMsg();
    }



}
